package com.izliang.gateway.controller;

import com.alibaba.fastjson.JSONObject;
import com.izliang.gateway.client.goods.GoodsClient;
import com.izliang.gateway.client.order.OrderClient;
import com.izliang.gateway.client.user.UserClient;
import model.Order;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import res.Result;

@RequestMapping(value = "/order")
@RestController
public class OrderController {

    @Autowired
    private OrderClient orderClient;

    @Autowired
    private UserClient userClient;

    @Autowired
    private GoodsClient goodsClient;

    @RequestMapping(value = "/info")
    public JSONObject info (int id){
        Order order = orderClient.getOrder(id);
        return Result.Result(200,"",order);
    }

    @RequestMapping(value = "/pay")
    public JSONObject pay(){
        try{
            //1.减库存
            goodsClient.subGoodsStock();
            //2.用户减钱
            userClient.subUserMoney();
            //3.生成订单
            orderClient.orderGenerate();
        }catch (Exception e){
            return Result.Result(400,e.getMessage(),"");
        }
        return Result.Result(200,"","");
    }

}
